/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 2-mei-2011, 21:36:47
 */

package trafficownage.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.UIManager;
import trafficownage.simulation.Car;
import trafficownage.simulation.MainLoop;
import trafficownage.simulation.MainLoopListener;
import trafficownage.simulation.TrafficManager.Mapping;
import trafficownage.util.Pair;
import trafficownage.util.StringFormatter;

/**
 *
 * @author Gerrit
 */
public class MainFrame extends javax.swing.JFrame implements MainLoopListener {

    private MainLoop m;

    private HashMap<JCheckBoxMenuItem,Mapping> benchmarkMappingMap;
    private List<Mapping> benchmarkMappings;

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();

        benchmarkMappingMap = new HashMap<JCheckBoxMenuItem,Mapping>();
        benchmarkMappings = new ArrayList<Mapping>();

        m = new MainLoop();
        m.init(this);

        updateInfo();

        initResultMenu();

        mapComponent2.init(m);

        mapComponent2.repaint();
    }

    private void initResultMenu() {
        for (Mapping mapping : m.getBenchmarkedMappings()) {
            resultsMenu.add(createMappingMenuItem(mapping));
        }
    }

    private ActionListener mappingCheckBoxMenuItemActionListener = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (!(e.getSource() instanceof JCheckBoxMenuItem))
                return;
            
            JCheckBoxMenuItem item = (JCheckBoxMenuItem)e.getSource();

            Mapping mapping = benchmarkMappingMap.get(item);

            if (mapping == null)
                return;

            if (item.isSelected() && !benchmarkMappings.contains(mapping))
                benchmarkMappings.add(mapping);
            else if (!item.isSelected() && benchmarkMappings.contains(mapping)) {
                benchmarkMappings.remove(mapping);
            }
        }

    };

    private JCheckBoxMenuItem createMappingMenuItem(Mapping mapping) {
        JCheckBoxMenuItem item = new JCheckBoxMenuItem();

        item.setText(mapping.getName());
        item.addActionListener(mappingCheckBoxMenuItemActionListener);
        item.setSelected(true);

        benchmarkMappings.add(mapping);

        benchmarkMappingMap.put(item, mapping);

        return item;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mapComponent1 = new trafficownage.ui.MapComponent();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        timeLabel1 = new javax.swing.JLabel();
        drawModeButtonGroup = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        timeLabel = new javax.swing.JLabel();
        carCountLabel = new javax.swing.JLabel();
        resultLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        mapComponent2 = new trafficownage.ui.MapComponent();
        jPanel3 = new javax.swing.JPanel();
        oldStartButton = new javax.swing.JButton();
        oldPauseButton = new javax.swing.JButton();
        oldStopButton = new javax.swing.JButton();
        increaseSimSpeedButton = new javax.swing.JButton();
        decreaseSimSpeedButton = new javax.swing.JButton();
        simSpeedLabel = new javax.swing.JLabel();
        currentViewModeLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        drawingMenu = new javax.swing.JMenu();
        drawingEnabledCheckbox = new javax.swing.JCheckBoxMenuItem();
        drawCarsRadioButton = new javax.swing.JRadioButtonMenuItem();
        drawDensityRadioButton = new javax.swing.JRadioButtonMenuItem();
        drawEmissionRadioButton = new javax.swing.JRadioButtonMenuItem();
        resultsMenu = new javax.swing.JMenu();

        javax.swing.GroupLayout mapComponent1Layout = new javax.swing.GroupLayout(mapComponent1);
        mapComponent1.setLayout(mapComponent1Layout);
        mapComponent1Layout.setHorizontalGroup(
            mapComponent1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        mapComponent1Layout.setVerticalGroup(
            mapComponent1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        timeLabel1.setFont(timeLabel1.getFont().deriveFont(timeLabel1.getFont().getStyle() | java.awt.Font.BOLD, timeLabel1.getFont().getSize()+3));
        timeLabel1.setText("     ");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Roadworx");

        jPanel2.setBackground(new java.awt.Color(102, 102, 102));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));

        timeLabel.setFont(timeLabel.getFont().deriveFont(timeLabel.getFont().getStyle() | java.awt.Font.BOLD, timeLabel.getFont().getSize()+3));
        timeLabel.setForeground(new java.awt.Color(255, 255, 255));
        timeLabel.setText("     ");

        carCountLabel.setFont(carCountLabel.getFont().deriveFont(carCountLabel.getFont().getStyle() | java.awt.Font.BOLD, carCountLabel.getFont().getSize()+3));
        carCountLabel.setForeground(new java.awt.Color(255, 255, 255));
        carCountLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        carCountLabel.setText("     ");

        resultLabel.setFont(resultLabel.getFont().deriveFont(resultLabel.getFont().getStyle() | java.awt.Font.BOLD, resultLabel.getFont().getSize()+3));
        resultLabel.setForeground(new java.awt.Color(255, 255, 255));
        resultLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        resultLabel.setText("     ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(carCountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addComponent(carCountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addComponent(resultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
        );

        jPanel4.setOpaque(false);

        javax.swing.GroupLayout mapComponent2Layout = new javax.swing.GroupLayout(mapComponent2);
        mapComponent2.setLayout(mapComponent2Layout);
        mapComponent2Layout.setHorizontalGroup(
            mapComponent2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 902, Short.MAX_VALUE)
        );
        mapComponent2Layout.setVerticalGroup(
            mapComponent2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 412, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 902, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mapComponent2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 412, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mapComponent2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel3.setBackground(new java.awt.Color(51, 51, 51));

        oldStartButton.setText("Start");
        oldStartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oldStartButtonActionPerformed(evt);
            }
        });

        oldPauseButton.setText("Pause");
        oldPauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oldPauseButtonActionPerformed(evt);
            }
        });

        oldStopButton.setText("Stop");
        oldStopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oldStopButtonActionPerformed(evt);
            }
        });

        increaseSimSpeedButton.setText("+");
        increaseSimSpeedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                increaseSimSpeedButtonActionPerformed(evt);
            }
        });

        decreaseSimSpeedButton.setText("-");
        decreaseSimSpeedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decreaseSimSpeedButtonActionPerformed(evt);
            }
        });

        simSpeedLabel.setFont(simSpeedLabel.getFont().deriveFont(simSpeedLabel.getFont().getStyle() | java.awt.Font.BOLD, simSpeedLabel.getFont().getSize()+1));
        simSpeedLabel.setForeground(new java.awt.Color(255, 255, 255));
        simSpeedLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        simSpeedLabel.setText("256x");

        currentViewModeLabel.setFont(currentViewModeLabel.getFont().deriveFont(currentViewModeLabel.getFont().getStyle() | java.awt.Font.BOLD, currentViewModeLabel.getFont().getSize()+3));
        currentViewModeLabel.setForeground(new java.awt.Color(255, 255, 255));
        currentViewModeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(oldStartButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(oldPauseButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(oldStopButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(currentViewModeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(decreaseSimSpeedButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(simSpeedLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(increaseSimSpeedButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(currentViewModeLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(oldStopButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(oldPauseButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(oldStartButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(increaseSimSpeedButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(simSpeedLabel)
                        .addComponent(decreaseSimSpeedButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        drawingMenu.setText("Drawing");

        drawingEnabledCheckbox.setSelected(true);
        drawingEnabledCheckbox.setText("Enabled");
        drawingEnabledCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                drawingEnabledCheckboxActionPerformed(evt);
            }
        });
        drawingMenu.add(drawingEnabledCheckbox);

        drawModeButtonGroup.add(drawCarsRadioButton);
        drawCarsRadioButton.setSelected(true);
        drawCarsRadioButton.setText("Cars");
        drawCarsRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                drawCarsRadioButtonActionPerformed(evt);
            }
        });
        drawingMenu.add(drawCarsRadioButton);

        drawModeButtonGroup.add(drawDensityRadioButton);
        drawDensityRadioButton.setText("Density");
        drawDensityRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                drawDensityRadioButtonActionPerformed(evt);
            }
        });
        drawingMenu.add(drawDensityRadioButton);

        drawModeButtonGroup.add(drawEmissionRadioButton);
        drawEmissionRadioButton.setText("CO2 emission");
        drawEmissionRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                drawEmissionRadioButtonActionPerformed(evt);
            }
        });
        drawingMenu.add(drawEmissionRadioButton);

        jMenuBar1.add(drawingMenu);

        resultsMenu.setText("Results");
        jMenuBar1.add(resultsMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void oldStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oldStartButtonActionPerformed
        m.start();
    }//GEN-LAST:event_oldStartButtonActionPerformed

    private void updateInfo() {
        simSpeedLabel.setText(m.getSpeedMultiplier() + "x");
        
        if (mapComponent2.isVisible()) {
            switch (mapComponent2.getDrawMode()) {
                case MapComponent.DRAW_CARS:
                    currentViewModeLabel.setText("Individual car view");
                    break;
                case MapComponent.DRAW_DENSITY:
                    currentViewModeLabel.setText("Traffic density view");
                    break;
                case MapComponent.DRAW_EMISSION:
                    currentViewModeLabel.setText("CO2 emission view");
                    break;
            }
        } else {
            currentViewModeLabel.setText("Drawing disabled.");
        }
    }


    private void oldPauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oldPauseButtonActionPerformed
        m.pause();
    }//GEN-LAST:event_oldPauseButtonActionPerformed

    private void oldStopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oldStopButtonActionPerformed
        m.stop();
    }//GEN-LAST:event_oldStopButtonActionPerformed

    private void increaseSimSpeedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_increaseSimSpeedButtonActionPerformed
        int speed = m.getSpeedMultiplier();

        speed = Math.min(256, speed + (int)Math.sqrt((double)speed));

        m.setSpeedMultiplier(speed);
        
        updateInfo();
    }//GEN-LAST:event_increaseSimSpeedButtonActionPerformed

    private void decreaseSimSpeedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_decreaseSimSpeedButtonActionPerformed
        int speed = m.getSpeedMultiplier();

        speed = Math.max(1, speed - (int)Math.sqrt((double)speed));

        m.setSpeedMultiplier(speed);

        updateInfo();
    }//GEN-LAST:event_decreaseSimSpeedButtonActionPerformed

    private void drawDensityRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drawDensityRadioButtonActionPerformed
        mapComponent2.setDrawMode(MapComponent.DRAW_DENSITY);

        updateInfo();
    }//GEN-LAST:event_drawDensityRadioButtonActionPerformed

    private void drawCarsRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drawCarsRadioButtonActionPerformed
        mapComponent2.setDrawMode(MapComponent.DRAW_CARS);

        updateInfo();
    }//GEN-LAST:event_drawCarsRadioButtonActionPerformed

    private void drawingEnabledCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drawingEnabledCheckboxActionPerformed
        mapComponent2.setVisible(drawingEnabledCheckbox.isSelected());

        updateInfo();
    }//GEN-LAST:event_drawingEnabledCheckboxActionPerformed

    private void drawEmissionRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_drawEmissionRadioButtonActionPerformed
        mapComponent2.setDrawMode(MapComponent.DRAW_EMISSION);

        updateInfo();
    }//GEN-LAST:event_drawEmissionRadioButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
         try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel carCountLabel;
    private javax.swing.JLabel currentViewModeLabel;
    private javax.swing.JButton decreaseSimSpeedButton;
    private javax.swing.JRadioButtonMenuItem drawCarsRadioButton;
    private javax.swing.JRadioButtonMenuItem drawDensityRadioButton;
    private javax.swing.JRadioButtonMenuItem drawEmissionRadioButton;
    private javax.swing.ButtonGroup drawModeButtonGroup;
    private javax.swing.JCheckBoxMenuItem drawingEnabledCheckbox;
    private javax.swing.JMenu drawingMenu;
    private javax.swing.JButton increaseSimSpeedButton;
    private javax.swing.JList jList1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private trafficownage.ui.MapComponent mapComponent1;
    private trafficownage.ui.MapComponent mapComponent2;
    private javax.swing.JButton oldPauseButton;
    private javax.swing.JButton oldStartButton;
    private javax.swing.JButton oldStopButton;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JMenu resultsMenu;
    private javax.swing.JLabel simSpeedLabel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JLabel timeLabel1;
    // End of variables declaration//GEN-END:variables


    public void benchmarkCarAdded(Car car) {
    }

    public void mapLoaded() {
        mapComponent2.init(m);
    }

    public Pair<Integer,Double> getResults() {
        double avg = 0.0;
        int count = 0;
        int counted = 0;

        for (Mapping mapping : benchmarkMappings) {
            if (mapping.getArrivedCarCount() > 0) {
                avg += mapping.getBenchmarkResults();
                count += mapping.getArrivedCarCount();
                counted++;
            }
        }
        if (counted > 0)
            avg /= (double)counted;
        
        return new Pair<Integer,Double>(count,avg);
    }

    private static final long UPDATE_TEXT_INTERVAL = 1000;
    private long previousUpdateTime = 0;
    private long currentUpdateTime = 0;

    public void nextFrame(double timestep) {
        currentUpdateTime = System.currentTimeMillis();

        if (currentUpdateTime - previousUpdateTime > UPDATE_TEXT_INTERVAL) {            
            previousUpdateTime = currentUpdateTime;

            timeLabel.setText(StringFormatter.getTimeString(m.getSimulationDayTime()));
            carCountLabel.setText(m.getCarCount() + " cars");

            resultLabel.setText("Current speed: " + StringFormatter.getTwoDecimalDoubleString(m.getCurrentSpeed()) + "x");

            /*if (mapComponent2.getDrawMode() == MapComponent.DRAW_EMISSION) {
                resultLabel.setText("Todays CO2 emission: " + (int)m.getCO2Emission() + " kg");
            } else {
                Pair<Integer,Double> results = getResults();
                resultLabel.setText("Score: " + results.getObject2() + "@" + results.getObject1() + " arrived cars.");
            }*/
        }

        if (drawingEnabledCheckbox.isSelected())
            mapComponent2.update();
        
    }

    public void logMessage(String message) {
        
    }

}
